package com.wipz.eoa.iphone.settlemanagement.expertssettle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.wipz.eoa.iphone.BaseServiceImpl;
import com.wipz.eoa.iphone.util.Constants;

/**
 * 
 * @author yangxue
 * 2012-2-10 下午14:05:10
 *
 */
public class ExpertsSettleServiceImpl extends BaseServiceImpl implements IExpertsSettleService{
	@Override
	public List getDetail(Map map) {
		List<Object> list = new ArrayList<Object>();
		Object obj = super.queryOne("enterprisesettle.detail", map);
		list.add(obj);
		Map parms = (Map) obj;
		parms.put("BILLCODE", parms.get("CODE"));
		Map processid =  (Map) super.queryOne("businessProcess.queryByCode", parms);
		list.add(processid);
		//定义业务类型ID集合
		Map<Object,Object> businessMap = new HashMap<Object,Object>();
		//将业务ID放入业务类型ID集合中
		businessMap.put("BUSINESSID", Constants.WORKENTRUST_BUSINESS_ID);
		List<Map> agencyByBusinessList  = super.queryList("agency.queryAgencyByBussinessID", businessMap);
		//定义 单位ID：单位名称 的集合
		Map<String,String> agencyByBusinessIdMap = new HashMap<String,String>();
		//遍历agencyByBusinessList，封装agencyByBusinessIdMap
		for (Map currentAgencyMap : agencyByBusinessList){
			String agencyID = (String)currentAgencyMap.get("AGENCYID");
			String name = (String)currentAgencyMap.get("NAME");
			agencyByBusinessIdMap.put(agencyID,name);
		}
		
		//定义用户ID集合
		Map<Object,Object> userIdMap = new HashMap<Object,Object>();
		//将用户ID放入用户ID集合中
		userIdMap.put("USERID", map.get("userId"));
		//根据用户ID查询所有单位ID集合
		List<Map> agencyByUserIdList = super.queryList("agency.queryAgencyByUserID",userIdMap);
		//定义 单位ID：单位名称 的集合
		Map<String,String> agencyByUserIdMap = new HashMap<String,String>();
		//遍历agencyByUserIdList，封装currentAgencyMap
		for (Map currentAgencyMap : agencyByUserIdList){
			String agencyID = (String)currentAgencyMap.get("AGENCYID");
			String name = (String)currentAgencyMap.get("NAME");
			agencyByUserIdMap.put(agencyID,name);
		}
		
		//取2个集合中的交集
		Map<String,String> agencyMap = new HashMap<String,String>();
		Iterator iterator = agencyByBusinessIdMap.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<String,String> entry = (Entry<String,String>)iterator.next();
			String key = entry.getKey();
			if (agencyByUserIdMap.containsKey(key)){
				String value = entry.getValue();
				agencyMap.put(key, value);
			}
		}
		list.add(agencyMap);
		//返回包含所有具有提交权限的单位集合
		return list;
	}

}
